#!/bin/bash
#这个脚本是用于:往存储管理系统post文件的

domainName="ccgl_dev.36ve.com"             #备份服务器的域名
port="52117"                                  #备份服务器的端口
fileDir="/lhwl/src/test/"  #需要备份的文件存放的目录
project=12                                  #这个项目在备份服务器上的项目id
storagetime=$(echo 365*10|bc)              #存储10年
uploadLogFile="/lhwl/bin/tmp/uploadLogFile.log" #上传记录,用于下次看看这个文件是否上传过
detailLogFile="/lhwl/bin/tmp/uploadLogFileDetail.log" #操作日志
mkdir -p /lhwl/bin/tmp/
isTest=true                           #是否为测试,默认是true

postUrl="http://${domainName}:${port}/index.php?r=home/addfile"   #http://ccgl_test.36ve.com:80/index.php?r=home/addfile



function killSelf(){
  ps -ef |grep `echo $0`|grep -v grep|awk -F ' ' '{print $2}'|xargs kill -9
}

function pass(){
  echo '' >> /dev/null
}
function getParameters() {
    if [ $# != 3 ]; then
        echo "./5.uploadFileToCCGLCentos7.sh -d=/lhwl/src/test/ -t=1 -p=13 是正确用法"
        killSelf
    fi
    for i in `echo $*`
    do
        key=`echo ${i}|awk -F '=' '{print $1}'`
        case ${key} in
          -t)
            storagetime=`echo ${i}|awk -F '=' '{print $2}'`
            ;;
          -d)
            fileDir=`echo ${i}|awk -F '=' '{print $2}'`
            ;;
          -p)
            project=`echo ${i}|awk -F '=' '{print $2}'`
            ;;
          *)
            echo "找不到"
        esac
    done
}
function logDetailLogFile() {
    echo '' >> ${detailLogFile}
    echo $1 >> ${detailLogFile}
    echo '' >> ${detailLogFile}
}
function uploadLog() {
    logDetailLogFile uploadLogFile.log
    curl -F "file1=@/lhwl/bin/tmp/uploadLogFile.log" -F "在原服务器中的相对路径=/lhwl/bin/tmp/uploadLogFile.log" -F "storagetime=3650" -F "project=${project}"  -F "curl=1" "${postUrl}"  >> ${detailLogFile}
    logDetailLogFile uploadLogFileDetail.log
    curl -F "file1=@/lhwl/bin/tmp/uploadLogFileDetail.log" -F "在原服务器中的相对路径=/lhwl/bin/tmp/uploadLogFileDetail.log" -F "storagetime=3650" -F "project=${project}"  -F "curl=1" "${postUrl}"  >> ${detailLogFile}
}
function rmOrPass() {
    if [ ${isTest} != true ]; then
        rm -f ${file}
    fi
}
function findAndPost() {
    echo `date +%Y%m%d_%H%M%S`   >> ${detailLogFile}
    fileList=`find ${fileDir} -type f`
    if [ "${fileList}"x = ""x ]; then
      uploadLog
      echo "记录为空"
      exit
    fi
    IFS=$(echo -en "\n\b") #为了处理文件名中的空格、TAB
    for file in `echo "${fileList}"`
      do
        #file='/lhwl/data/upload/gczyk.36ve.com_test/xxpt/res3/u58293/20189918211,邱韬睿，根据信用证内容回答问题.doc'
        md5=`md5sum ${file}|awk -F ' ' '{print $1}'`
        echo "开始处理:" ${file} +++ "${md5}">> ${detailLogFile}
        if [ `grep ${md5} ${uploadLogFile}|grep ${file}`  ]; then #如果能查出来,说明已经上传过了:就可以跳过了,否就要上传
            pass
            echo "这个文件且这个md5值,已经推送过一次了,本次跳过 ++ ${md5} ++++  ${file} " >> ${detailLogFile}
        else
          #curl -F "file1=@3.mp4" -F "在原服务器中的相对路径=files/class/3.mp4" -F "storagetime=1" -F "project=9"  -F "curl=1" "http://ccgl_test.36ve.com/index.php?r=home/addfile"
          shell="curl -F \"file1=@${file}\" -F \"在原服务器中的相对路径=${file}\" -F \"storagetime=${storagetime}\" -F \"project=${project}\"  -F \"curl=1\" \"${postUrl}\""
          echo ${shell} >> ${detailLogFile}
          fileParamer=`echo file1=@\"${file}\"` #解决文件名中有逗号的问题
          result=`curl -F ${fileParamer} -F "在原服务器中的相对路径=${file}" -F "storagetime=${storagetime}" -F "project=${project}"  -F "curl=1" "${postUrl}"`
          #result=`curl -F "file1=@${file}" -F "在原服务器中的相对路径=${file}" -F "storagetime=${storagetime}" -F "project=${project}"  -F "curl=1" "${postUrl}"`

          echo "返回值是:" "'"${result}"'" >> ${detailLogFile}
          if [ ${result} == '数据库中已经有一模一样的数据了,不能重复上传' ]; then
            rmOrPass
            continue
          fi
          resultMd5=`echo "'"${result}"'"|awk -F "+++|'" '{print $3}'`
          echo "返回值中的md5值是:" ${resultMd5} >> ${detailLogFile}

          if [ ${resultMd5}x == ""x ]; then
              sleep 300
              echo "返回值中的没有md5值,睡眠300s,跳过本记录" >> ${detailLogFile}
              continue
              #echo "返回值中的没有md5值,停止程序" >> ${detailLogFile}
              #killSelf
          fi
          if [ ${resultMd5} != ${md5} ]; then

              echo "返回值中的md5值是:"${resultMd5}",与源文件的md5值:"${md5}"不同,停止程序" >> ${detailLogFile}
              killSelf
          fi
          if [ `echo ${result}|grep ${domainName} ` ]; then
              echo ${md5} ++++  ${file} +++++ ${result} >> ${uploadLogFile}
              rmOrPass
              echo "上传成功,已删除:" ${file}  >> ${detailLogFile}
          else
            echo "上传有问题:"  ${file} >> ${detailLogFile}
            killSelf
          fi
        fi
        echo '#####################' >> ${detailLogFile}
        #exit
      done
    uploadLog
    echo `date +%Y%m%d_%H%M%S`   >> ${detailLogFile}
}

getParameters $*
echo "本次备份的时长是:" ${storagetime} >> ${detailLogFile}
echo "本次备份的目录是:" ${fileDir} >> ${detailLogFile}
echo "本次备份的项目id是:" ${project} >> ${detailLogFile}

findAndPost

#清空记录的方法
#echo 1> /lhwl/bin/tmp/uploadLogFile.log  ;echo 1> /lhwl/bin/tmp/uploadLogFileDetail.log